package jcommon.chap5_collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.list.PredicatedList;
import org.apache.commons.collections.map.TypedMap;

// 或者叫“Constraint”
public class Predicated {

	public static void main(String[] args) {
		System.out.println("-------- Test TypedCollection --------");
		typedCollectionDemo();
		System.out.println("-------- Test TypedMap --------");
		typedMapDemo();
		System.out.println("-------- Test PredicatedMap --------");
		predicatedMapDemo();
		System.out.println("-------- Test PredicatedCollection --------");
		predicatedCollectionDemo();
		System.out.println("-------- Test PredicatedList --------");
		predicatedListDemo();
	}
	
	public static void typedCollectionDemo() {
		Map typedMap = TypedMap.decorate( new HashMap( ), String.class, Number.class );

		// This will add a String key and a Double value
		typedMap.put( "TEST", new Double( 3.40 ) );

		// Both of these throw an IllegalArgumentException
		typedMap.put( new Long(202), new Double( 3.40 ) );
		typedMap.put( "BLAH", "BLAH" );
	}
	
	public static void typedMapDemo() {
		
	}
	
	public static void predicatedMapDemo() {
		
	}
	
	public static void predicatedCollectionDemo() {
		
	}
	
	public static void predicatedListDemo() {
		// Create a Predicate that only accepts even Integer objects
		Predicate onlyEven = new Predicate( ) {
		    public boolean evaluate(Object object) {
		        Integer integer = (Integer) object;
		        return( integer.intValue( ) % 2 == 0 );
		    }
		};

		List list = PredicatedList.decorate( new ArrayList( ), onlyEven );
		list.add( new Integer(1) ); // Will throw IllegalArgumentException
		list.add( new Integer(2) );
		list.add( new Integer(3) ); // Will throw IllegalArgumentException
		list.add( new Integer(4) );
	}
	
	// ... 还有各种predicatedXXX
}
